﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class TrainSchemeEdit : System.Web.UI.Page
    {
        protected TrainScheme trainScheme;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (!string.IsNullOrEmpty(formNo))
                {
                    trainScheme = ctx.TrainSchemes.Single(r => r.FormNo == formNo);
                    empName = ctx.Employees.Single(r => r.Id == trainScheme.EmpId).Name;
                    txtTime.Value = trainScheme.Time;
                    txtCourse.Value = trainScheme.Course;
                    txtPeriod.Value = trainScheme.Period;
                    txtType.Value = trainScheme.Type;
                    txtMethods.Value = trainScheme.Methods;
                    txtPrincipal.Value = trainScheme.Principal;
                    txtJoiners.Value = trainScheme.Joiners;
                    txtBudget.Value = trainScheme.Budget;
                    txtEffectEval.Value = trainScheme.EffectEval;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    string deptName = ctx.Depts.Single(r => r.Id == ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId).Text;
                    empName = ebi.Name;
                    trainScheme = new TrainScheme();
                    trainScheme.CreatDate = DateTime.Now;
                    trainScheme.DeptName = deptName;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            if (string.IsNullOrEmpty(formNo))
            {
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                trainScheme = new TrainScheme();
                string maxFormNo = ctx.TrainSchemes.Max(r => r.FormNo);
                formNo = "Form_Tsem_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Tsem_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                trainScheme.FormNo = formNo;
                trainScheme.CreatDate = DateTime.Now;
                trainScheme.EmpId = ebi.Id;
                string deptName = ctx.Depts.Single(r => r.Id == ebi.EmpDepPos.OrderByDescending(r2 => r2.PosId).First().DeptId).Text;
                trainScheme.DeptName = deptName;
                trainScheme.NeedsYear = 2012;
                trainScheme.StatusId = 1;
                ctx.TrainSchemes.InsertOnSubmit(trainScheme);
            }
            else
            {
                trainScheme = ctx.TrainSchemes.Single(r => r.FormNo == formNo);
                trainScheme.UpdateDate = DateTime.Now;
            }
            trainScheme.Time = txtTime.Value;
            trainScheme.Course = txtCourse.Value;
            trainScheme.Period = txtPeriod.Value;
            trainScheme.Type = txtType.Value;
            trainScheme.Methods = txtMethods.Value;
            trainScheme.Principal = txtPrincipal.Value;
            trainScheme.Joiners = txtJoiners.Value;
            trainScheme.Budget = txtBudget.Value;
            trainScheme.EffectEval = txtEffectEval.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
        }
    }
}